/**
 * 
 */
package org.mhz.sit.util;

import java.io.IOException;
import java.net.URL;

import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Paint;
import javafx.stage.Modality;
import javafx.stage.Stage;

import org.mhz.sit.exception.SpeakInTimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Mahan
 *
 * Alert utility class
 */
public class AlertUtil
{
    private static final Logger LOGGER = LoggerFactory.getLogger(ValidationUtil.class);
    
    private static Stage alertStage;
    private static String alertMessage;
    
    /**
     * Private constructor
     */
    private AlertUtil()
    {
        
    }
    
    /**
     * Display an alert
     *
     * @param aMessage - the message to display
     */
    public static void displayAlert(final String aMessage) 
    {
        try
        {
            setAlertMessage(aMessage);
            
            final URL url = AlertUtil.class.getClassLoader().getResource(Constants.ALERT_FXML);
            final Parent root = FXMLLoader.load(url);
            
            alertStage = new Stage();
            alertStage.initModality(Modality.APPLICATION_MODAL);
            alertStage.setTitle(Constants.ALERT_TITLE);
            final Scene scene = new Scene(root, Paint.valueOf(Constants.BACKGROUND));
            scene.getStylesheets().add(Constants.MAIN_STYLESHEET);
            alertStage.setScene(scene);
            alertStage.show();
            
            throw new SpeakInTimeException(aMessage);
        }
        catch (IOException ioEx)
        {
            LOGGER.error("Unable to open alert dialog", ioEx);
        }
    }
    
    /**
     * Set the alert message
     *
     * @param aAlertMessage - The alert message to set
     */
    public static void setAlertMessage(final String aAlertMessage)
    {
        alertMessage = aAlertMessage;
    }
   
    /**
     * The alert message
     *
     * @return the alert message
     */
    public static String getAlertMessage()
    {
        return alertMessage;
    }

    /**
     * @return Returns the alertStage.
     */
    public static Stage getAlertStage()
    {
        return alertStage;
    }         
}
